import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QEvent


class Demo(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('PyQt5事件处理 - 窗口关闭事件')
        self.setGeometry(300, 300, 400, 200)

        self.button = QPushButton('关闭窗口', self)
        self.button.move(150, 70)

        self.show()

    def closeEvent(self, event):
        reply = QMessageBox.question(self, '提示', '是否要退出程序？', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = Demo()
    sys.exit(app.exec_())